package com.slz.crm.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slz.crm.common.annotation.RequirePermission;
import com.slz.crm.common.enumeration.PermissionOperates;
import com.slz.crm.common.result.Result;
import com.slz.crm.pojo.dto.ContactTaskDTO;
import com.slz.crm.pojo.vo.ContactTaskVO;
import com.slz.crm.server.service.ContactTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 联络任务控制器
 */
@RestController
@RequestMapping("/contactTask")
@Slf4j
public class ContactTaskController {

    @Autowired
    private ContactTaskService contactTaskService;

    /**
     * 创建联络任务
     */
    @PostMapping("/create")
    @RequirePermission(
            permClass = PermissionOperates.TaskOperate.class,
            permName = "CREATE_TASK"
    )
    public Result<Boolean> create(@RequestBody ContactTaskDTO contactTaskDTO) {
        Boolean result = contactTaskService.create(contactTaskDTO);
        return Result.success(result);
    }

    /**
     * 批量删除联络任务
     */
    @DeleteMapping("/batch")
    @RequirePermission(
            permClass = PermissionOperates.TaskOperate.class,
            permName = "DELETE_TASK"
    )
    public Result<Integer> deleteByIds(@RequestBody List<Long> idList) {
        Integer count = contactTaskService.deleteByIds(idList);
        return Result.success(count);
    }

    /**
     * 删除单个联络任务
     */
    @DeleteMapping("/{id}")
    @RequirePermission(
            permClass = PermissionOperates.TaskOperate.class,
            permName = "DELETE_TASK"
    )
    public Result<Boolean> deleteById(@PathVariable Long id) {
        Boolean result = contactTaskService.deleteById(id);
        return Result.success(result);
    }

    /**
     * 批量更新联络任务
     */
    @PutMapping("/batch")
    @RequirePermission(
            permClass = PermissionOperates.TaskOperate.class,
            permName = "UPDATE_TASK"
    )
    public Result<Integer> updateList(@RequestBody List<ContactTaskDTO> contactTaskDTOList) {
        Integer count = contactTaskService.updateList(contactTaskDTOList);
        return Result.success(count);
    }

    /**
     * 更新单个联络任务
     */
    @PutMapping("/update")
    @RequirePermission(
            permClass = PermissionOperates.TaskOperate.class,
            permName = "UPDATE_TASK"
    )
    public Result<Boolean> update(@RequestBody ContactTaskDTO contactTaskDTO) {
        Boolean result = contactTaskService.update(contactTaskDTO);
        return Result.success(result);
    }

    /**
     * 根据ID查询联络任务详情
     */
    @GetMapping("/{id}")
    @RequirePermission(
            permClass = PermissionOperates.TaskOperate.class,
            permName = "VIEW_TASK"
    )
    public Result<ContactTaskVO> getById(@PathVariable Long id) {
        ContactTaskVO contactTaskVO = contactTaskService.getById(id);
        return Result.success(contactTaskVO);
    }

    /**
     * 分页查询所有联络任务
     */
    @GetMapping("/all")
    @RequirePermission(
            permClass = PermissionOperates.TaskOperate.class,
            permName = "VIEW_TASK"
    )
    public Result<Page<ContactTaskVO>> getAll(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<ContactTaskVO> page = contactTaskService.getAll(pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 根据条件分页查询联络任务
     */
    @PostMapping("/query")
    @RequirePermission(
            permClass = PermissionOperates.TaskOperate.class,
            permName = "VIEW_TASK"
    )
    public Result<Page<ContactTaskVO>> query(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody ContactTaskDTO contactTaskDTO) {
        Page<ContactTaskVO> page = contactTaskService.query(pageNum, pageSize, contactTaskDTO);
        return Result.success(page);
    }

    /**
     * 根据执行人ID查询任务列表
     */
    @GetMapping("/assignee/{assigneeId}")
    @RequirePermission(
            permClass = PermissionOperates.TaskOperate.class,
            permName = "VIEW_TASK"
    )
    public Result<List<ContactTaskVO>> getByAssigneeId(@PathVariable Long assigneeId) {
        List<ContactTaskVO> tasks = contactTaskService.getByAssigneeId(assigneeId);
        return Result.success(tasks);
    }

    /**
     * 根据公司ID查询任务列表
     */
    @GetMapping("/company/{companyId}")
    @RequirePermission(
            permClass = PermissionOperates.TaskOperate.class,
            permName = "VIEW_TASK"
    )
    public Result<List<ContactTaskVO>> getByCompanyId(@PathVariable Long companyId) {
        List<ContactTaskVO> tasks = contactTaskService.getByCompanyId(companyId);
        return Result.success(tasks);
    }

    /**
     * 根据联系人ID查询任务列表
     */
    @GetMapping("/contact/{contactId}")
    @RequirePermission(
            permClass = PermissionOperates.TaskOperate.class,
            permName = "VIEW_TASK"
    )
    public Result<List<ContactTaskVO>> getByContactId(@PathVariable Long contactId) {
        List<ContactTaskVO> tasks = contactTaskService.getByContactId(contactId);
        return Result.success(tasks);
    }

    /**
     * 根据销售机会ID查询任务列表
     */
    @GetMapping("/opportunity/{opportunityId}")
    @RequirePermission(
            permClass = PermissionOperates.TaskOperate.class,
            permName = "VIEW_TASK"
    )
    public Result<List<ContactTaskVO>> getByOpportunityId(@PathVariable Long opportunityId) {
        List<ContactTaskVO> tasks = contactTaskService.getByOpportunityId(opportunityId);
        return Result.success(tasks);
    }
}